# -*- coding: utf-8 -*-
# @File  : app01.py
# @Author:  liubinyu
# @Time: 2024/5/19 11:39
from typing import Union, Optional

from fastapi import APIRouter, File, UploadFile
from typing import List

app05 = APIRouter()

# file: bytes = File()：适合小文件上传
@app05.post("/files/")
async def create_file(file: bytes = File()):
    print("file:", file)
    return {"file_size": len(file)}

@app05.post("/multiFiles/")
async def create_files(files: List[bytes] = File()):
    return {"file_sizes": [len(file) for file in files]}


# file: UploadFile：适合大文件上传

@app05.post("/uploadFile/")
async def create_upload_file(file: UploadFile):
    with open(f"{file.filename}", 'wb') as f:
        for chunk in iter(lambda: file.file.read(1024), b''):
            f.write(chunk)
    return {"filename": file.filename}

@app05.post("/multiUploadFiles/")
async def create_upload_files(files: List[UploadFile]):
    return {"filenames": [file.filename for file in files]}